﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Hs.CardUt.D3
{
    /// <summary>
    ///德卡D3-U
    /// </summary>
    public class D3_Card
    {
        // Token: 0x06000036 RID: 54 RVA: 0x0000352C File Offset: 0x0000172C
        public static int OpenPort()
        {
            if (icdev < 0)
            {
                icdev = D3Port.dc_init(100, 115200);
                if (icdev <= 0)
                {
                    icdev = D3Port.dc_init(9, 115200);
                    if (icdev <= 0)
                    {
                        return 0;
                    }
                }
            }
            return icdev;
        }

        // Token: 0x06000037 RID: 55 RVA: 0x00003598 File Offset: 0x00001798
        public static int ClosePort()
        {
            return icdev;
        }

        // Token: 0x06000039 RID: 57 RVA: 0x000038DC File Offset: 0x00001ADC
        public string AuthenticationCard(int cardKind)
        {
            string sdata = Tools.GetAsciiConvertHexdecimal("JZKJ" + cardKind.ToString()).PadRight(32, '0');
            string text = "";
            ulong num = 0UL;
            st = (int)D3Port.dc_card(icdev, '0', ref num);
            string result;
            if (st != 0)
            {
                text += "1.寻卡失败!";
                result = text;
            }
            else
            {
                text += "1.寻卡成功!";
                st = D3Port.dc_beep(icdev, 50);
                if (st != 0)
                {
                    text += "2.蜂鸣失败!";
                    result = text;
                }
                else
                {
                    text += "2.蜂鸣成功!";
                    st = D3Port.dc_authentication_passaddr_hex(icdev, 0, 15, "FFFFFFFFFFFF");
                    if (st != 0)
                    {
                        text += "3.校验密码失败!";
                        result = text;
                    }
                    else
                    {
                        text += "3.校验密码成功!";
                        st = D3Port.dc_write_hex(icdev, 14, sdata);
                        if (st != 0)
                        {
                            text += "4.卡类型写入失败!";
                            result = text;
                        }
                        else
                        {
                            text += "4.卡类型写入成功!";
                            st = D3Port.dc_write_hex(icdev, 15, "2B385F6D1A4EFF078069FFFFFFFFFFFF");
                            if (st != 0)
                            {
                                text += "5.3区密码修改失败!";
                                result = text;
                            }
                            else
                            {
                                text += "5.3区密码修改成功!";
                                result = text + "|1";
                            }
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 发卡，写卡
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="cardUserName"></param>
        /// <param name="cardKind"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public string WriteCard(string cardNo, string cardUserName, int cardKind, DateTime start, DateTime end, int state)
        {
            icdev = OpenPort();
            string result;
            if (icdev == 0)
            {
                result = "0,读卡器端口初始化失败!";
            }
            else
            {
                //text += "0,端口初始化成功!";
                ulong num = 0UL;
                st = (int)D3Port.dc_card(icdev, '0', ref num);
                if (st != 0)
                {
                    result = "1,读卡器寻卡失败!";
                }
                else
                {
                    //text += "1.寻卡成功!";
                    st = D3Port.dc_beep(icdev, 50);
                    if (st != 0)
                    {
                        result = "2,读卡器蜂鸣失败!";
                    }
                    else
                    {
                        //text += "2.蜂鸣成功!";
                        st = D3Port.dc_authentication_passaddr_hex(icdev, 0, 15, "2B385F6D1A4E");
                        if (st != 0)
                        {
                            result = "3,校验密码失败!";
                        }
                        else
                        {
                            //text += "3.校验密码成功!";
                            StringBuilder stringBuilder = new StringBuilder();
                            st = (int)D3Port.dc_read_hex(icdev, 14, stringBuilder);
                            if (st != 0)
                            {
                                result = "3,1.读取卡类型失败!";
                            }
                            else
                            {
                                //text += "3.1.读取卡类型成功!";
                                string hexdecimalConvertAscii = Tools.GetHexdecimalConvertAscii(stringBuilder.ToString());
                                if (!hexdecimalConvertAscii.Contains("JZKJ"))
                                {
                                    result = "3,-----未认证用户卡!|-1";
                                }
                                else if (hexdecimalConvertAscii.ToString().Substring(4, 1) != cardKind.ToString())
                                {
                                    result = "3,-----发卡类型与认证卡时类型不符!|-2";
                                }
                                else
                                {
                                    st = D3Port.dc_write_hex(icdev, 12, cardNo.PadRight(32, '0'));
                                    if (st != 0)
                                    {
                                        result = "4,卡号写入失败!";
                                    }
                                    else
                                    {
                                        //text += "4,卡号写入成功!";
                                        st = D3Port.dc_write_hex(icdev, 13, Tools.GetAsciiConvertHexdecimal(cardUserName + cardKind.ToString()).PadRight(32, '0'));
                                        if (st != 0)
                                        {
                                            result = "5,用户名写入失败!";
                                        }
                                        else
                                        {
                                            //text += "5,用户名写入成功!";
                                            st = D3Port.dc_authentication_passaddr_hex(icdev, 0, 19, "FFFFFFFFFFFFFF078069FFFFFFFFFFFF");
                                            if (st != 0)
                                            {
                                                result = "6,校验密码失败!";
                                            }
                                            else
                                            {
                                                //text += "6,校验密码成功!";
                                                st = D3Port.dc_write_hex(icdev, 16, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
                                                if (st != 0)
                                                {
                                                    result = "7,开卡日期回收失败!";
                                                }
                                                else
                                                {
                                                    //text += "7,开卡日期回收成功!";
                                                    st = D3Port.dc_write_hex(icdev, 17, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
                                                    if (st != 0)
                                                    {
                                                        result = "8,卡有效期结束时间回收失败!";
                                                    }
                                                    else
                                                    {
                                                        //text += "8,卡有效期结束时间回收成功!";
                                                        st = D3Port.dc_write_hex(icdev, 18, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
                                                        if (st != 0)
                                                        {
                                                            result = "9,卡状态回收失败!";
                                                        }
                                                        else
                                                        {
                                                            //text += "9,卡号回收成功!";
                                                            st = D3Port.dc_write_hex(icdev, 19, "AD8DF9CB5119FF078069FFFFFFFFFFFF");
                                                            if (st != 0)
                                                            {
                                                                result = "10,4区密码加密失败!";
                                                            }
                                                            else
                                                            {
                                                                //text += "10.4区密码加密成功!";
                                                                ClosePort();
                                                                result = "8888,写卡成功";
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 读取卡片信息
        /// </summary>
        /// <returns></returns>
        public static string ReadCardNo()
        {
            string text = "";
            icdev = OpenPort();
            string result;
            if (icdev == 0)
            {
                text += "0.端口初始化失败!";
                result = text;
            }
            else
            {
                text += "0.端口初始化成功!";
                ulong num = 0UL;
                st = (int)D3Port.dc_card(icdev, '0', ref num);
                if (st != 0)
                {
                    text += "0,1.寻卡失败!";
                    result = text;
                }
                else
                {
                    text += "1.寻卡成功!";
                    st = D3Port.dc_beep(icdev, 50);
                    if (st != 0)
                    {
                        text += ",2.蜂鸣失败!";
                        result = text;
                    }
                    else
                    {
                        text += "2.蜂鸣成功!";
                        st = D3Port.dc_authentication_passaddr_hex(icdev, 0, 15, "2B385F6D1A4E");
                        if (st != 0)
                        {
                            text += ",3.校验密码失败!";
                            result = text;
                        }
                        else
                        {
                            text += "3.校验密码成功!";
                            StringBuilder stringBuilder = new StringBuilder();
                            st = (int)D3Port.dc_read_hex(icdev, 12, stringBuilder);

                            if (st != 0)
                            {
                                text += ",4.读取卡号失败!";
                                result = text;
                            }
                            else
                            {
                                text += "4.读取卡号成功!";
                                ClosePort();
                                result = stringBuilder.ToString().Substring(0, 16);
                            }
                        }
                    }
                }
            }
            return result;
        }

        // Token: 0x0600003C RID: 60 RVA: 0x00003FB0 File Offset: 0x000021B0
        public static string ReadCard(out string cardNo, out int cardType)
        {
            cardNo = "";
            cardType = -1;
            return QueryCard(out cardNo, out cardType);
        }

        // Token: 0x0600003D RID: 61 RVA: 0x00003FD4 File Offset: 0x000021D4
        public static string QueryCard(out string cardNo, out int cardType)
        {
            cardNo = "";
            cardType = -1;
            string text = "";
            icdev = OpenPort();
            string result;
            if (icdev == 0)
            {
                text += "0.端口初始化失败!";
                result = text;
            }
            else
            {
                text += "0.端口初始化成功!";
                ulong num = 0UL;
                st = (int)D3Port.dc_card(icdev, '0', ref num);
                if (st != 0)
                {
                    text += ",1.寻卡失败!";
                    result = text;
                }
                else
                {
                    st = D3Port.dc_beep(icdev, 50);
                    if (st != 0)
                    {
                        text += ",2.蜂鸣失败!";
                        result = text;
                    }
                    else
                    {
                        st = D3Port.dc_authentication_passaddr_hex(icdev, 0, 15, "2B385F6D1A4E");
                        if (st != 0)
                        {
                            text += ",3.校验密码失败!";
                            result = text;
                        }
                        else
                        {
                            StringBuilder stringBuilder = new StringBuilder();
                            st = (int)D3Port.dc_read_hex(icdev, 12, stringBuilder);
                            if (st != 0)
                            {
                                text += ",4.0.读取卡号失败!";
                                result = text;
                            }
                            else
                            {
                                StringBuilder stringBuilder2 = new StringBuilder();
                                st = (int)D3Port.dc_read_hex(icdev, 14, stringBuilder2);
                                if (st != 0)
                                {
                                    text += ",4.1.读取卡类型失败!";
                                    result = text;
                                }
                                else
                                {
                                    cardNo = stringBuilder.ToString().Substring(0, 16);
                                    string hexdecimalConvertAscii = Tools.GetHexdecimalConvertAscii(stringBuilder2.ToString());
                                    cardType = Convert.ToInt32(hexdecimalConvertAscii.ToString().Substring(4, 1));
                                    ClosePort();
                                    result = "8888,读卡成功";
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }

        public static string ReadCardsnr(out int ret, out string cardNo)
        {
            cardNo = "";
            return QueryCardsnr(out ret, out cardNo);
        }

        public static string QueryCardsnr(out int ret, out string cardNo)
        {
            try
            {
                ret = 0;
                cardNo = "";
                string text = "";
                icdev = OpenPort();
                string result;
                if (icdev == 0)
                {
                    text += "0.端口初始化失败!";
                    result = text;
                    ret = 0;
                }
                else
                {
                    text += "0.端口初始化成功!";
                    ulong num = 0UL;
                    st = (int)D3Port.dc_card(icdev, '0', ref num);
                    if (st != 0)
                    {
                        text += ",1.寻卡失败!";
                        result = text;
                        ret = 1;
                    }
                    else
                    {
                        st = D3Port.dc_beep(icdev, 50);
                        if (st != 0)
                        {
                            text += ",2.蜂鸣失败!";
                            result = text;
                            ret = 2;
                        }
                        else
                        {
                            long snr = 0;
                            StringBuilder stringBuilder = new StringBuilder();
                            st = (int)D3Port.dc_card(icdev, '1', out snr);
                            if (st != 0)
                            {
                                text += ",4.0.读取卡号失败!";
                                result = text;
                                ret = 4;
                            }
                            else
                            {
                                StringBuilder stringBuilder2 = new StringBuilder();
                                st = (int)D3Port.dc_card(icdev, '1', out snr);
                                if (st != 0)
                                {
                                    text += ",4.1.读取卡类型失败!";
                                    result = text;
                                }
                                else
                                {
                                    cardNo = snr.ToString("X");
                                    // cardNo = cardNo.Substring(6, 2) + cardNo.Substring(4, 2) + cardNo.Substring(2, 2) + cardNo.Substring(0, 2);
                                    ClosePort();
                                    result = "8888,读卡成功";
                                    ret = 9;
                                }
                            }
                        }
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                ret = 4;
                cardNo = "";
                return "0,读卡异常";
            }
        }

        // Token: 0x04000023 RID: 35
        private const string btCardNo = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

        // Token: 0x04000024 RID: 36
        private const string btCardUserName = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

        // Token: 0x04000025 RID: 37
        private const string btCardKind = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

        // Token: 0x04000026 RID: 38
        private const string btKey3 = "FFFFFFFFFFFFFF078069FFFFFFFFFFFF";

        // Token: 0x04000027 RID: 39
        private const string btCardStartDate = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

        // Token: 0x04000028 RID: 40
        private const string btCardEndDate = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

        // Token: 0x04000029 RID: 41
        private const string btCardState = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

        // Token: 0x0400002A RID: 42
        private const string btKey4 = "FFFFFFFFFFFFFF078069FFFFFFFFFFFF";

        // Token: 0x0400002B RID: 43
        private const string strKey3 = "2B385F6D1A4EFF078069FFFFFFFFFFFF";

        // Token: 0x0400002C RID: 44
        private const string strKey4 = "AD8DF9CB5119FF078069FFFFFFFFFFFF";

        // Token: 0x0400002D RID: 45
        private static int icdev = -1;

        // Token: 0x0400002E RID: 46
        private static int st = -1;

        // Token: 0x0400002F RID: 47
        private string strCardNo = "";

        // Token: 0x04000030 RID: 48
        private string strCardUserName = "";
    }
}
